package com.liuchuanzheng.lcz_wanandroid.module.home.adapter

import android.view.ViewGroup.MarginLayoutParams
import android.widget.CheckedTextView
import androidx.core.view.updateLayoutParams
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.liuchuanzheng.lcz_wanandroid.R
import com.liuchuanzheng.lcz_wanandroid.ext.dpToPx
import com.liuchuanzheng.lcz_wanandroid.ext.htmlToSpanned
import com.liuchuanzheng.lcz_wanandroid.module.home.bean.Category

class CategoryAdapter(layoutResId: Int = R.layout.item_category_sub) :
    BaseQuickAdapter<Category, BaseViewHolder>(layoutResId) {

    private var checkedPosition = 0

    override fun convert(holder: BaseViewHolder, item: Category) {
        holder.itemView.run {
            findViewById<CheckedTextView>(R.id.ctvCategory).text = item.name.htmlToSpanned()
            findViewById<CheckedTextView>(R.id.ctvCategory).isChecked = checkedPosition == holder.adapterPosition
            updateLayoutParams<MarginLayoutParams> {
                marginStart =
                    if (holder.adapterPosition == 0) 8.dpToPx().toInt() else 0.dpToPx().toInt()
            }
        }
    }

    fun check(position: Int) {
        checkedPosition = position
        notifyDataSetChanged()
    }

}